package com.qf.service.impl;

import com.qf.dto.DeveloperDto;
import com.qf.mapper.DeveloperMapper;
import com.qf.model.Developer;
import com.qf.model.Project;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.service.DeveloperService;
import com.qf.service.ProjectService;
import com.qf.utils.UuidUtils;
import com.qf.vo.DevProVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Service
public class DeveloperServiceImpl implements DeveloperService {
    @Autowired
    private DeveloperMapper developerMapper;
    @Autowired
    private ProjectService projectService;

    @Override
    public List<Developer> getAll() {
        List<Developer> developers= developerMapper.findAll();
        return developers;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageResult pageResult = new PageResult();
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        List<Developer> list = developerMapper.findPage((currentPage-1)*pageSize,pageSize);
        Long count = developerMapper.count();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    @Transactional//json方式添加
    public int addDeveloper(DevProVO devProVO) {
        Developer developer = new Developer();
        developer.setDid(UuidUtils.uuid());
        developer.setDname(devProVO.getDname());
        developer.setUpdatetime(new Date());
        int row = developerMapper.add(developer);
        projectService.bindDid(developer.getDid(),devProVO.getPids());
        return row;
    }

    @Override
    @Transactional  //fromencode方式提交参数
    //添加开发商的同时还要将工程进行绑定
    public int addAndBind(String did, String dname, int[] pids) {
        //添加开发商
        Developer developer =new Developer();
        developer.setDid(UuidUtils.uuid());
        developer.setDname(dname);
        developer.setUpdatetime(new Date());
        int row = developerMapper.add(developer);
        //同时更新project的did
        projectService.deleteDid(did);
        //绑定工程
        projectService.bindDid(developer.getDid(),pids);
        return row;
    }



    @Override//开发商数据回显
    public DeveloperDto findByDid(String did) {
        //id,pname
        DeveloperDto developerDto = developerMapper.findByDidDto(did);
        //pids[]
        List<Project> list = projectService.findByDid(did);
        int[] pids=null;
        if (ObjectUtils.isNotEmpty(list)){
            pids =  new int[list.size()];
            for (int i=0;i<list.size();i++){
                pids[i]=list.get(i).getPid();
            }
            developerDto.setPids(pids);
        }
        return developerDto;
    }

    @Override
    @Transactional
    public int edit(DeveloperDto developerDto) {
        int row = developerMapper.edit(developerDto);
        //同时更新project的did
        projectService.deleteDid(developerDto.getDid());
        return row;
    }

    @Override
    @Transactional
    //删除逻辑：删除开发商的时候同时，要将工程列表中对应的的开发商id设置为null
    public int deleteByDid(String did) {
        int row = developerMapper.delByDid(did);
        //更新project中的did
        projectService.deleteDid(did);
        return row;
    }



}
